#!/bin/bash
# 获取脚本所在目录的绝对路径
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# 设置 PROJECT_ROOT 为脚本目录的上一级目录
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
cd "$PROJECT_ROOT" || { echo "无法进入目录: $PROJECT_ROOT"; exit 1; }

# 定义目录变量
YAML_CPP_SOURCE="${PROJECT_ROOT}/yaml-cpp"
INSTALL_DIR="${PROJECT_ROOT}/lib/yaml-cpp"

# 清理旧构建
echo "清理旧构建文件..."
rm -rf "${YAML_CPP_SOURCE}/build"
mkdir -p "${YAML_CPP_SOURCE}/build" && cd "${YAML_CPP_SOURCE}/build"

# 配置CMake
echo "配置CMake..."
cmake .. \
  -DCMAKE_INSTALL_PREFIX="${INSTALL_DIR}" \
  -DYAML_CPP_BUILD_TESTS=OFF \
  -DYAML_CPP_BUILD_TOOLS=OFF \
  -DYAML_BUILD_SHARED_LIBS=ON \
  -DCMAKE_BUILD_TYPE=Release

# 编译并安装
echo "编译安装中..."
make -j$(nproc) && make install

# 验证安装
echo "安装完成！检查输出目录："
ls -l "${INSTALL_DIR}"